<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>vue-入门Day.04 | 让技术更简单</title>

  <!-- keywords -->
  

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="如何解决axios中的跨域问题使用代理服务器proxy步骤：1.在服务端设置白名单2.设置一个本地代理（服务端代理proxy），用代理服务器请求接口，再通过代理服务器将数据发送给浏览器，从而解决跨域问题2-1.因为这个文件是通过vue-cli脚手架创立的，需要手动创建vue.config.js创建在根目录,这是vue脚手架的官方配置文件，关于打包的信息都可以通过vue.config.js进行配置。">
<meta property="og:type" content="article">
<meta property="og:title" content="vue-入门Day.04">
<meta property="og:url" content="http://example.com/2022/03/19/vue-day-04/index.html">
<meta property="og:site_name" content="让技术更简单">
<meta property="og:description" content="如何解决axios中的跨域问题使用代理服务器proxy步骤：1.在服务端设置白名单2.设置一个本地代理（服务端代理proxy），用代理服务器请求接口，再通过代理服务器将数据发送给浏览器，从而解决跨域问题2-1.因为这个文件是通过vue-cli脚手架创立的，需要手动创建vue.config.js创建在根目录,这是vue脚手架的官方配置文件，关于打包的信息都可以通过vue.config.js进行配置。">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2022-03-19T10:37:29.000Z">
<meta property="article:modified_time" content="2022-03-21T06:04:46.939Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternative" href="/atom.xml" title="让技术更简单" type="application/atom+xml">
  
  
    <link rel="icon" href="http://7xkj1z.com1.z0.glb.clouddn.com/head.jpg">
  
  
<link rel="stylesheet" href="/css/style.css">

  
  

  
<script src="//cdn.bootcss.com/require.js/2.3.2/require.min.js"></script>

  
<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>


  
<meta name="generator" content="Hexo 6.1.0"></head>
<body>
  <div id="container">
    <div id="particles-js"></div>
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
	<header id="header" class="inner">
		<a href="/" class="profilepic">
			
			<img lazy-src="http://7xkj1z.com1.z0.glb.clouddn.com/head.jpg" class="js-avatar">
			
		</a>

		<hgroup>
		  <h1 class="header-author"><a href="/"></a></h1>
		</hgroup>

		

		
			<div class="switch-btn">
				<div class="icon">
					<div class="icon-ctn">
						<div class="icon-wrap icon-house" data-idx="0">
							<div class="birdhouse"></div>
							<div class="birdhouse_holes"></div>
						</div>
						<div class="icon-wrap icon-ribbon hide" data-idx="1">
							<div class="ribbon"></div>
						</div>
						
						<div class="icon-wrap icon-link hide" data-idx="2">
							<div class="loopback_l"></div>
							<div class="loopback_r"></div>
						</div>
						
						
					</div>
					
				</div>
				<div class="tips-box hide">
					<div class="tips-arrow"></div>
					<ul class="tips-inner">
						<li>菜单</li>
						<li>标签</li>
						
						<li>友情链接</li>
						
						
					</ul>
				</div>
			</div>
		

		<div class="switch-area">
			<div class="switch-wrap">
				<section class="switch-part switch-part1">
					<nav class="header-menu">
						<ul>
						
							<li><a href="/">主页</a></li>
				        
							<li><a href="/archives">所有文章</a></li>
				        
						</ul>
					</nav>
					<nav class="header-nav">
						<div class="social">
							
						</div>
					</nav>
				</section>
				
				
				<section class="switch-part switch-part2">
					<div class="widget tagcloud" id="js-tagcloud">
						
					</div>
				</section>
				
				
				
				<section class="switch-part switch-part3">
					<div id="js-friends">
					
			          <a target="_blank" class="main-nav-link switch-friends-link" href="https://github.com/smackgg/hexo-theme-smackdown">smackdown</a>
			        
			        </div>
				</section>
				

				
			</div>
		</div>
	</header>				
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
  	<div class="overlay">
  		<div class="slider-trigger"></div>
  		<h1 class="header-author js-mobile-header hide"></h1>
  	</div>
	<div class="intrude-less">
		<header id="header" class="inner">
			<div class="profilepic">
				<img lazy-src="http://7xkj1z.com1.z0.glb.clouddn.com/head.jpg" class="js-avatar">
			</div>
			<hgroup>
			  <h1 class="header-author"></h1>
			</hgroup>
			
			<nav class="header-menu">
				<ul>
				
					<li><a href="/">主页</a></li>
		        
					<li><a href="/archives">所有文章</a></li>
		        
		        <div class="clearfix"></div>
				</ul>
			</nav>
			<nav class="header-nav">
				<div class="social">
					
				</div>
			</nav>
		</header>				
	</div>
</nav>
      <div class="body-wrap"><article id="post-vue-day-04" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/2022/03/19/vue-day-04/" class="article-date">
  	<time datetime="2022-03-19T10:37:29.000Z" itemprop="datePublished">2022-03-19</time>
</a>
    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      vue-入门Day.04
      
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        
        

        
        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="如何解决axios中的跨域问题"><a href="#如何解决axios中的跨域问题" class="headerlink" title="如何解决axios中的跨域问题"></a>如何解决axios中的跨域问题</h2><h3 id="使用代理服务器proxy"><a href="#使用代理服务器proxy" class="headerlink" title="使用代理服务器proxy"></a>使用代理服务器proxy</h3><p>步骤：<br><strong>1</strong>.在服务端设置白名单<br><strong>2</strong>.设置一个本地代理（服务端代理proxy），用代理服务器请求接口，再通过代理服务器将数据发送给浏览器，从而解决跨域问题<br><strong>2-1</strong>.因为这个文件是通过vue-cli脚手架创立的，需要手动创建<code>vue.config.js</code>创建在根目录,这是vue脚手架的官方配置文件，关于打包的信息都可以通过vue.config.js进行配置。<br><strong>2-2</strong>.因为vue.config.js是属于nodejs的，所以要遵守nodejs协议，通过commenJS去导出，导出一个对象，<strong>devServer</strong>是用来设置本地开发服务器的一个方法.<br><strong>2-3</strong>:看接口的地址</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">components<span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">    <span class="comment">//components是一个方法，使用子组件</span></span><br><span class="line">    VueList<span class="punctuation">,</span></span><br><span class="line">  <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">  <span class="comment">// 过程：客户端发起请求，被代理服务器拦截，并通过代理服务转发到目标服务器上</span></span><br><span class="line">  mounted()<span class="punctuation">&#123;</span></span><br><span class="line">    this.$axios.get(&#x27;/test&#x27;<span class="punctuation">,</span><span class="punctuation">&#123;</span></span><br><span class="line">      params<span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line"></span><br><span class="line">      <span class="punctuation">&#125;</span></span><br><span class="line">    <span class="punctuation">&#125;</span>).then((res)=&gt;<span class="punctuation">&#123;</span></span><br><span class="line">      console.log(res);</span><br><span class="line">      <span class="comment">// 获取数据</span></span><br><span class="line">    <span class="punctuation">&#125;</span>).catch((err)=&gt;<span class="punctuation">&#123;</span></span><br><span class="line">      console.error(err);</span><br><span class="line">    <span class="punctuation">&#125;</span>);</span><br><span class="line">    <span class="comment">// 捕获错误</span></span><br><span class="line">  <span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#125;</span>;</span><br></pre></td></tr></table></figure>

<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// vue-config.js</span></span><br><span class="line">module.exports=<span class="punctuation">&#123;</span></span><br><span class="line">  <span class="comment">// 开发服务</span></span><br><span class="line">  devServer<span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">    <span class="comment">// 代理，拦截请求，客户端发起请求，被代理服务器拦截，并通过代理服务转发到目标服务器上</span></span><br><span class="line">    proxy<span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">      <span class="comment">// 如果请求接口携带test</span></span><br><span class="line">      <span class="attr">&quot;/test&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">        target<span class="punctuation">:</span><span class="string">&quot;http://192.168.0.94:8080&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="comment">// 只需要写目标服务器地址，不需要写路径，路径在客户端写。</span></span><br><span class="line">        changeOrigin<span class="punctuation">:</span><span class="keyword">true</span><span class="punctuation">,</span></span><br><span class="line">        pathRewrite<span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">          <span class="comment">// 将以/api为开头的路径中的/api替换成空字符串,相当于加了一个前缀。</span></span><br><span class="line">          <span class="comment">// 如果接口中有api则：</span></span><br><span class="line">          <span class="comment">// &#x27;^/api&#x27;: &#x27;/api&#x27;,</span></span><br><span class="line">          <span class="comment">// 如果接口中没有api，则：</span></span><br><span class="line">          <span class="attr">&quot;^/api&quot;</span><span class="punctuation">:</span><span class="string">&quot;&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">      <span class="punctuation">&#125;</span></span><br><span class="line">    <span class="punctuation">&#125;</span></span><br><span class="line">  <span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 创建的接口</span></span><br><span class="line">const koa=require(&#x27;koa&#x27;);</span><br><span class="line">const Router=require(&#x27;koa-router&#x27;);</span><br><span class="line">const app=new koa();</span><br><span class="line">const router=new Router();</span><br><span class="line">router.get(&#x27;/test&#x27;<span class="punctuation">,</span>(ctx)=&gt;<span class="punctuation">&#123;</span></span><br><span class="line">    ctx.body=&#x27;这是首页&#x27;;</span><br><span class="line"><span class="punctuation">&#125;</span>)</span><br><span class="line">app.use(router.routes());</span><br><span class="line">app.use(router.allowedMethods());</span><br><span class="line">app.listen(<span class="number">8080</span><span class="punctuation">,</span>()=&gt;<span class="punctuation">&#123;</span></span><br><span class="line">    console.log(&#x27;running.....&#x27;);</span><br><span class="line"><span class="punctuation">&#125;</span>)</span><br></pre></td></tr></table></figure>

<h2 id="常用状态码"><a href="#常用状态码" class="headerlink" title="常用状态码"></a>常用状态码</h2><h3 id="2XX——表明请求被正常处理了"><a href="#2XX——表明请求被正常处理了" class="headerlink" title="2XX——表明请求被正常处理了"></a>2XX——表明请求被正常处理了</h3><p><strong>200</strong>OK：请求已正常处理。</p>
<p><strong>204</strong> No Content：请求处理成功，但没有任何资源可以返回给客户端，一般在只需要从客户端往服务器发送信息，而对客户端不需要发送新信息内容的情况下使用。</p>
<h3 id="3XX——表明浏览器需要执行某些特殊的处理以正确处理请求（重定向）"><a href="#3XX——表明浏览器需要执行某些特殊的处理以正确处理请求（重定向）" class="headerlink" title="3XX——表明浏览器需要执行某些特殊的处理以正确处理请求（重定向）"></a>3XX——表明浏览器需要执行某些特殊的处理以正确处理请求（重定向）</h3><p><strong>301</strong> Moved Permanently：资源的uri已更新，你也更新下你的书签引用吧。永久性重定向，请求的资源已经被分配了新的URI，以后应使用资源现在所指的URI。</p>
<p><strong>302</strong> Found：资源的URI已临时定位到其他位置了，姑且算你已经知道了这个情况了。临时性重定向。和301相似，但302代表的资源不是永久性移动，只是临时性性质的。换句话说，已移动的资源对应的URI将来还有可能发生改变。</p>
<p>当301,302,303响应状态码返回时，几乎所有的浏览器都会把POST改成GET，并删除请求报文内的主体，之后请求会自动再次发送。</p>
<h3 id="4XX——表明客户端是发生错误的原因所在。"><a href="#4XX——表明客户端是发生错误的原因所在。" class="headerlink" title="4XX——表明客户端是发生错误的原因所在。"></a>4XX——表明客户端是发生错误的原因所在。</h3><p><strong>400</strong> Bad Request：服务器端无法理解客户端发送的请求，请求报文中可能存在语法错误。</p>
<p><strong>401</strong> Unauthorized：该状态码表示发送的请求需要有通过HTTP认证（BASIC认证，DIGEST认证）的认证信息。</p>
<p><strong>403</strong> Forbidden：不允许访问那个资源。该状态码表明对请求资源的访问被服务器拒绝了。（权限，未授权IP等）</p>
<p><strong>404</strong> Not Found：服务器上没有请求的资源。路径错误等。</p>
<h3 id="5XX——服务器本身发生错误"><a href="#5XX——服务器本身发生错误" class="headerlink" title="5XX——服务器本身发生错误"></a>5XX——服务器本身发生错误</h3><p><strong>500</strong> Internal Server Error：貌似内部资源出故障了。该状态码表明服务器端在执行请求时发生了错误。也有可能是web应用存在bug或某些临时故障。</p>
<p><strong>503</strong> Service Unavailable：抱歉，我现在正在忙着。该状态码表明服务器暂时处于超负载或正在停机维护，现在无法处理请求。</p>
<h2 id="在form表单中请求的数据格式"><a href="#在form表单中请求的数据格式" class="headerlink" title="在form表单中请求的数据格式"></a>在form表单中请求的数据格式</h2><p>在在form表单中请求的数据格式为formData，如：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">form</span> <span class="attr">action</span>=<span class="string">&quot;http://192.168.0.108:8081/api/text&quot;</span> <span class="attr">method</span>=<span class="string">&quot;get&quot;</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;password&quot;</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;submit&quot;</span> <span class="attr">value</span>=<span class="string">&quot;提交&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">form</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">this.$axios.post(&#x27;/text&#x27;<span class="punctuation">,</span><span class="punctuation">&#123;</span></span><br><span class="line">      data<span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">        name<span class="punctuation">:</span><span class="string">&quot;123&quot;</span><span class="punctuation">,</span></span><br><span class="line">        age<span class="punctuation">:</span><span class="number">18</span><span class="punctuation">,</span></span><br><span class="line">      <span class="punctuation">&#125;</span></span><br><span class="line">    <span class="punctuation">&#125;</span>).then((res)=&gt;<span class="punctuation">&#123;</span></span><br><span class="line">      console.log(res);</span><br><span class="line">      <span class="comment">// 获取数据</span></span><br><span class="line">    <span class="punctuation">&#125;</span>).catch((err)=&gt;<span class="punctuation">&#123;</span></span><br><span class="line">      console.error(err);</span><br><span class="line">    <span class="punctuation">&#125;</span>);</span><br></pre></td></tr></table></figure>

<h2 id="transformRequest（转化拦截数据的格式）"><a href="#transformRequest（转化拦截数据的格式）" class="headerlink" title="transformRequest（转化拦截数据的格式）"></a>transformRequest（转化拦截数据的格式）</h2><p>在传数据时，需要使用代理服务器时采用的格式可能需要转换为其它格式时可以用到这个方法。<br>如：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 使用post方法传数据时，需要使用代理服务器时采用的格式。</span></span><br><span class="line">    this.$axios(<span class="punctuation">&#123;</span></span><br><span class="line">      method<span class="punctuation">:</span>&#x27;post&#x27;<span class="punctuation">,</span></span><br><span class="line">      url<span class="punctuation">:</span>&#x27;/api/text&#x27;<span class="punctuation">,</span></span><br><span class="line">      data<span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">        name<span class="punctuation">:</span>&#x27;<span class="number">123</span>&#x27;</span><br><span class="line">      <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">      <span class="comment">// transformRequest（转化拦截数据的格式）</span></span><br><span class="line">      transformRequest<span class="punctuation">:</span><span class="punctuation">[</span></span><br><span class="line">        function(data<span class="punctuation">,</span>headers)<span class="punctuation">&#123;</span></span><br><span class="line">          <span class="comment">// qs相当于拦截的数据，同时qs是内置的，需要引入。</span></span><br><span class="line">          return qs.stringify(data);</span><br><span class="line">        <span class="punctuation">&#125;</span></span><br><span class="line">      <span class="punctuation">]</span></span><br><span class="line">    <span class="punctuation">&#125;</span>)</span><br></pre></td></tr></table></figure>

<h2 id="axios-create（）"><a href="#axios-create（）" class="headerlink" title="axios.create（）"></a>axios.create（）</h2><p>axios.create（）方法是对axios的二次封装，对axios的实例化操作</p>
<h3 id="使用方式"><a href="#使用方式" class="headerlink" title="使用方式"></a>使用方式</h3><p><code>1.</code> 在根目录中创建一个文件夹，一般命名为request，之后在文件夹中创建一个js文件<br><code>2.</code> 在这个文件中引入axios和qs，qs主要用来处理数据的转换，相当于一个数据。<br><code>3</code> 在这个文件中对axios.create进行实例化操作，用一个变量接受，axios.create中为一个对象，主要参数为地址的前面部分，时间，数据转换<br><code>4.</code> 在main.js中引入这个js文件</p>
<p>例子：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// axios.js中的代码</span></span><br><span class="line">import axios from <span class="string">&quot;axios&quot;</span>;</span><br><span class="line">import qs from <span class="string">&quot;qs&quot;</span>;</span><br><span class="line"></span><br><span class="line">const han=axios.create(<span class="punctuation">&#123;</span></span><br><span class="line">    baseURL<span class="punctuation">:</span>&#x27;/api&#x27;<span class="punctuation">,</span></span><br><span class="line">    transformRequest<span class="punctuation">:</span><span class="punctuation">[</span></span><br><span class="line">        function(data<span class="punctuation">,</span>headers)<span class="punctuation">&#123;</span></span><br><span class="line">          return qs.stringify(data);</span><br><span class="line">        <span class="punctuation">&#125;</span></span><br><span class="line">    <span class="punctuation">]</span><span class="punctuation">,</span></span><br><span class="line">    timeout<span class="punctuation">:</span><span class="number">4000</span><span class="punctuation">,</span></span><br><span class="line"><span class="punctuation">&#125;</span>);</span><br><span class="line">export default han;</span><br></pre></td></tr></table></figure>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// main.js中的代码</span></span><br><span class="line">import han from &#x27;../request/axios.js&#x27;;</span><br><span class="line">Vue.prototype.$axios=han;</span><br></pre></td></tr></table></figure>

<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// vue中的代码</span></span><br><span class="line">this.$axios.post(&#x27;/text&#x27;<span class="punctuation">,</span><span class="punctuation">&#123;</span></span><br><span class="line">      data<span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">        name<span class="punctuation">:</span><span class="string">&quot;123&quot;</span><span class="punctuation">,</span></span><br><span class="line">        age<span class="punctuation">:</span><span class="number">18</span><span class="punctuation">,</span></span><br><span class="line">      <span class="punctuation">&#125;</span></span><br><span class="line">    <span class="punctuation">&#125;</span>).then((res)=&gt;<span class="punctuation">&#123;</span></span><br><span class="line">      console.log(res);</span><br><span class="line">      <span class="comment">// 获取数据</span></span><br><span class="line">    <span class="punctuation">&#125;</span>).catch((err)=&gt;<span class="punctuation">&#123;</span></span><br><span class="line">      console.error(err);</span><br><span class="line">    <span class="punctuation">&#125;</span>);</span><br></pre></td></tr></table></figure>
<h2 id="拦截器（interceptors）"><a href="#拦截器（interceptors）" class="headerlink" title="拦截器（interceptors）"></a>拦截器（interceptors）</h2><p>transformRequest是拦截请求数据和请求数据头的，而interceptors是拦截请求和响应的。</p>
<h3 id="请求拦截器"><a href="#请求拦截器" class="headerlink" title="请求拦截器"></a>请求拦截器</h3><p>可以拿到数据的状态吗，在请求之前拦截请求，对请求的数据进行相应的处理</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">han.interceptors.request.use(function (config) <span class="punctuation">&#123;</span></span><br><span class="line">    <span class="comment">// 在发送请求之前做些什么</span></span><br><span class="line">    return config;</span><br><span class="line">  <span class="punctuation">&#125;</span><span class="punctuation">,</span> function (error) <span class="punctuation">&#123;</span></span><br><span class="line">    <span class="comment">// 对请求错误做些什么</span></span><br><span class="line">    <span class="comment">// 错误包含了状态码</span></span><br><span class="line">    console.log(error<span class="punctuation">,</span>&#x27;---------------&#x27;);</span><br><span class="line">    <span class="comment">// 拿到状态码，对状态码进行相应的处理</span></span><br><span class="line">    return Promise.reject(&#x27;出错了&#x27;);</span><br><span class="line">  <span class="punctuation">&#125;</span>);</span><br></pre></td></tr></table></figure>

<h3 id="响应拦截器"><a href="#响应拦截器" class="headerlink" title="响应拦截器"></a>响应拦截器</h3><p>可以拿到数据的状态吗，在数据得到响应之前拦截响应，对状态码进行相应的处理</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 添加请求拦截器</span></span><br><span class="line">han.interceptors.response.use(function (config) <span class="punctuation">&#123;</span></span><br><span class="line">    <span class="comment">// 在发送请求之前做些什么</span></span><br><span class="line">    return config;</span><br><span class="line">  <span class="punctuation">&#125;</span><span class="punctuation">,</span> function (error) <span class="punctuation">&#123;</span></span><br><span class="line">    <span class="comment">// 对请求错误做些什么</span></span><br><span class="line">    <span class="comment">// 错误包含了状态码</span></span><br><span class="line">    console.log(error<span class="punctuation">,</span>&#x27;---------------&#x27;);</span><br><span class="line">    <span class="comment">// 拿到状态码，对状态码进行相应的处理</span></span><br><span class="line">    return Promise.reject(&#x27;出错了&#x27;);</span><br><span class="line">  <span class="punctuation">&#125;</span>);</span><br></pre></td></tr></table></figure>
      
    </div>
    
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2022/03/21/vue-day05/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">&lt;</strong>
      <div class="article-nav-title">
        
          vue-入门Day.05
        
      </div>
    </a>
  
  
    <a href="/2022/03/17/vue-day-03/" id="article-nav-older" class="article-nav-link-wrap">
      <div class="article-nav-title">vue-入门Day.03</div>
      <strong class="article-nav-caption">&gt;</strong>
    </a>
  
</nav>

  
</article>


<div class="ds-share share" data-thread-key="vue-day-04" data-title="vue-入门Day.04" data-url="http://example.com/2022/03/19/vue-day-04/"  data-images="http://7xkj1z.com1.z0.glb.clouddn.com/head.jpg" data-content="vue-入门Day.04">
    <div class="ds-share-inline">
      <ul  class="ds-share-icons-16">
      	<li data-toggle="ds-share-icons-more"><a class="ds-more" href="javascript:void(0);">分享到：</a></li>
        <li><a class="ds-weibo" href="javascript:void(0);" data-service="weibo">微博</a></li>
        <li><a class="ds-qzone" href="javascript:void(0);" data-service="qzone">QQ空间</a></li>
        <li><a class="ds-qqt" href="javascript:void(0);" data-service="qqt">腾讯微博</a></li>
        <li><a class="ds-wechat" href="javascript:void(0);" data-service="wechat">微信</a></li>
      </ul>
      <div class="ds-share-icons-more">
      </div>
    </div>
 </div>
 





</div>
      <footer id="footer">
  <div class="outer">
    <div id="footer-info">
      <div class="footer-left">
        &copy; 2022 John Doe
      </div>
        <div class="footer-right">
          <a href="http://hexo.io/" target="_blank">Hexo</a>  Theme <a href="https://github.com/smackgg/hexo-theme-smackdown" target="_blank">Smackdown</a>
        </div>
    </div>
  </div>
</footer>
    </div>
    
  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">



<script>
	var yiliaConfig = {
		fancybox: true,
		mathjax: true,
		animate: true,
		isHome: false,
		isPost: true,
		isArchive: false,
		isTag: false,
		isCategory: false,
		open_in_new: false
	}
</script>

<script src="/js/main.js"></script>




<script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    }
});

MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';                 
    }       
});
</script>

<script src="//cdn.bootcss.com/mathjax/2.7.0/MathJax.js"></script>


  </div>
</body>
</html>